package com.heiyu.utils;

import java.util.Date;
import java.util.Random;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;

public class CodeUtils {

	private static final String	CODE_LIST	= "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789";

	public static String getId(Long no) {
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		String target = StringUtils.leftPad(String.valueOf(no % 100), 4, "0");
		sb = sb.append(DateUtils.format(new Date(), "yyyyMMdd")).append("00").append(target).append(StringUtils.leftPad(String.valueOf(r.nextInt(10000)), 4, "0"));
		return sb.toString();
	}

	public static String getOrderNo(String prdType, Long no) {
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		String target = StringUtils.leftPad(String.valueOf(no % 100000000), 8, "0");
		sb = sb.append(DateUtils.format(new Date(), "yyyyMMdd")).append("00").append(prdType).append(target).append(StringUtils.leftPad(String.valueOf(r.nextInt(10000)), 4, "0"));
		return sb.toString();
	}

	public static String getOrderNoAndDate(String prdType, Long no, Date date) {
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		String target = StringUtils.leftPad(String.valueOf(no % 100000000), 8, "0");
		sb = sb.append(DateUtils.format(date, "yyyyMMdd")).append(prdType).append(target).append(StringUtils.leftPad(String.valueOf(r.nextInt(10000)), 4, "0"));
		return sb.toString();
	}

	public static String getyyMMddSerno(Long no) {
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		String target = StringUtils.leftPad(String.valueOf(no % 100000000), 8, "0");
		sb = sb.append(DateUtils.format(new Date(), "yyyyMMdd")).append("00").append(target).append(StringUtils.leftPad(String.valueOf(r.nextInt(10000)), 4, "0"));
		return sb.toString();
	}

	public static String getyyMMddRoyaltyno(Long no) {
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		String target = StringUtils.leftPad(String.valueOf(no % 1000000), 6, "0");
		sb = sb.append(DateUtils.format(new Date(), "yyyyMMdd")).append(target).append(StringUtils.leftPad(String.valueOf(r.nextInt(1000)), 3, "0"));
		return sb.toString();
	}

	public static Long sortNum(String idno) {
		return Long.parseLong(idno.substring(idno.length() - 12, idno.length() - 4));
	}

	public static String getUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	/**
	 * 把号码转成字符串 0-9A-Za-z 如10 A 11 B 36 a
	 */
	public static String idToCode(String id, Integer subLen) {
		if (subLen < 8) {
			subLen = 8;
		}
		String subStr = id.substring(id.length() - subLen);
		return idToCode(Long.valueOf(subStr));
	}

	/**
	 * 把号码转成字符串 0-9A-Za-z 如10 A 11 B 36 a
	 */
	public static String idToCode(Long no) {
		if (no < 10) {
			return String.valueOf(no);
		} else if (no < 36) {
			return String.valueOf((char) (no - 10 + 'A'));
		} else if (no < 62) {
			return String.valueOf((char) (no - 36 + 'a'));
		} else {
			return idToCode(no / 62) + idToCode(no % 62);
		}
	}

}
